/**
 * This project is a database of food additives, denoted code
 * E.
 * 
 * Copyright (C) 2010  Dmitry Zolotaryov
 *
 * This program is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * http://www.gnu.org/licenses/.
 * 
 * Contact the author:
 * e-mail: zdv2010@gmail.com
 * ICQ: 476-467-654
 * Jabber: c0unt_zer0@jabber.org
 */

package zdv.foodadditives;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

/**
 * @author Dmitry Zolotaryov
 * This is a main class of the application
 */

public class FoodAdditives extends Activity implements OnClickListener {
	private ArrayList<MyItem> myItems;
	private ListView myListView;
	private EditText editSearch;
	private Button btnSearch;
	private MyItemAdapter adapter;
	
	MyDBAdapter dbAdapter;
	Cursor cursor;
	
	private static final int MENU_SHOW_ALL = Menu.FIRST;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myListView = (ListView)findViewById(R.id.myListView);
        editSearch = (EditText)findViewById(R.id.editSearch);
        btnSearch = (Button)findViewById(R.id.btnSearch);
        
        btnSearch.setOnClickListener(this);
        myItems = new ArrayList<MyItem>();
        adapter = new MyItemAdapter(this, R.layout.item, myItems);
        myListView.setAdapter(adapter);
        dbAdapter = new MyDBAdapter(this);
        dbAdapter.open();	
        populate();
    }

    /**
     * 
     */
	public void populate() {
		cursor = dbAdapter.getAllEntries();
		startManagingCursor(cursor);
		updateArray();
	}

	/**
	 * 
	 */
	public void updateArray() {
		cursor.requery();
		myItems.clear();
		if(cursor.moveToFirst()){
			do {
				String name = cursor.getString(cursor.getColumnIndex(MyDBAdapter.KEY_NAME));
				String name_ru = cursor.getString(cursor.getColumnIndex(MyDBAdapter.KEY_NAME_RU));
				int categorie = cursor.getInt(cursor.getColumnIndex(MyDBAdapter.KEY_CATEGORIE));
				int danger = cursor.getInt(cursor.getColumnIndex(MyDBAdapter.KEY_DANGER));
				String description = cursor.getString(cursor.getColumnIndex(MyDBAdapter.KEY_DESCRIPTION));
				String description_ru = cursor.getString(cursor.getColumnIndex(MyDBAdapter.KEY_DESCRIPTION_RU));
				String code = cursor.getString(cursor.getColumnIndex(MyDBAdapter.KEY_CODE));
				MyItem item = new MyItem(name, name_ru, categorie, danger,
						description, description_ru, code);
				myItems.add(0, item);
			} while(cursor.moveToNext());
			adapter.notifyDataSetChanged();
		}
	}

	public void onClick(View arg0) {
		if(arg0.equals(btnSearch)){
			String strSearch = editSearch.getText().toString();
			MyItem item = dbAdapter.getEntry(strSearch);
			if(item != null){
				showEntry(item);
			}
		}
	}
	
	/**
	 * displays the found record
	 * @param entry - found record
	 */
	private void showEntry(MyItem entry){
		myItems.clear();
		myItems.add(0, entry);
		adapter.notifyDataSetChanged();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		dbAdapter.close();
	}
	
	@Override
	public void onPause() {
		super.onPause();
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, MENU_SHOW_ALL, Menu.NONE, R.string.menu_show_all);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
		case MENU_SHOW_ALL: populate(); return true;
		}
		return false;
	}
}